package com.santalu.myapplication.slice;

import cn.bingoogolapple.refreshlayout.BgaStickinessRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BgarefreshLayout;
import com.santalu.emptyview.EmptyView;
import com.santalu.emptyview.MenuView;
import com.santalu.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * MainAbilitySlice
 *
 * @author hw
 * @since 2021/5/24
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener,
        BgarefreshLayout.BgarefreshLayoutDelegate {
    private EmptyView emptyView;
    private BgarefreshLayout refreshLayout;
    private Image imgTopBar;
    private EventHandler eventHandler;
    private MenuView menuView;

    private final Runnable showContentTask = new Runnable() {
        @Override
        public void run() {
            emptyView.showContent();
        }
    };

    @Override
    public void onStart(Intent intent) {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);   // 沉浸式状态栏
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main_ohos);
        initComponent();
    }

    private void initComponent() {
        if (findComponentById(ResourceTable.Id_refresh_layout) instanceof BgarefreshLayout) {
            refreshLayout = (BgarefreshLayout) findComponentById(ResourceTable.Id_refresh_layout);
        }
        if (findComponentById(ResourceTable.Id_empty_view) instanceof EmptyView) {
            emptyView = (EmptyView) findComponentById(ResourceTable.Id_empty_view);
        }
        if (findComponentById(ResourceTable.Id_image_topbar) instanceof Image) {
            imgTopBar = (Image) findComponentById(ResourceTable.Id_image_topbar);
            imgTopBar.setClickedListener(this);
        }
        if (findComponentById(ResourceTable.Id_menuView) instanceof MenuView) {
            menuView = (MenuView) findComponentById(ResourceTable.Id_menuView);
            menuView.findComponentById(ResourceTable.Id_show_progress).setClickedListener(this);
            menuView.findComponentById(ResourceTable.Id_show_content).setClickedListener(this);
            menuView.findComponentById(ResourceTable.Id_show_error).setClickedListener(this);
            menuView.findComponentById(ResourceTable.Id_show_empty).setClickedListener(this);
        }
        eventHandler = new EventHandler(EventRunner.getMainEventRunner());
        initRefreshLayout();
    }

    private void initRefreshLayout() {
        refreshLayout.setDelegate(this);
        refreshLayout.setIsShowLoadingMoreView(true);
        refreshLayout.setPullDownRefreshEnable(true);
        BgaStickinessRefreshViewHolder stickinessRefreshViewHolder = new BgaStickinessRefreshViewHolder(this,
                false);
        stickinessRefreshViewHolder.setPullDownRefreshText("");
        stickinessRefreshViewHolder.setUltimateColor(ResourceTable.Color_black);
        stickinessRefreshViewHolder.setLoaderTextColor(ResourceTable.Color_black);
        stickinessRefreshViewHolder.setPullUpDilogueVisible(false);
        stickinessRefreshViewHolder.setLoaderTextSize(50);
        stickinessRefreshViewHolder.setProgressSize(90);
        refreshLayout.setRefreshViewHolder(stickinessRefreshViewHolder);
        refreshLayout.endRefreshing();
    }

    private void showLoading() {
        emptyView.showLoading();
        eventHandler.postTask(showContentTask, 2000);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_topbar:
                menuView.openMenu();
                break;
            case ResourceTable.Id_show_progress:
                emptyView.loading().show();
                if (menuView != null) {
                    menuView.dismiss();
                }
                break;
            case ResourceTable.Id_show_empty:
                emptyView.empty()
                        .setOnClickListener(view -> showLoading())
                        .show();
                if (menuView != null) {
                    menuView.dismiss();
                }
                break;
            case ResourceTable.Id_show_error:
                emptyView.error()
                        .setOnClickListener(view -> MainAbilitySlice.this.showLoading())
                        .show();
                if (menuView != null) {
                    menuView.dismiss();
                }
                break;
            case ResourceTable.Id_show_content:
                emptyView.content().show();
                if (menuView != null) {
                    menuView.dismiss();
                }
                break;
        }
    }

    @Override
    public void onBgarefreshLayoutBeginRefreshing(BgarefreshLayout bgarefreshLayout) {
        showLoading();
    }

    @Override
    public boolean onBgarefreshLayoutBeginLoadingMore(BgarefreshLayout bgarefreshLayout) {
        return false;
    }
}
